package com.appkubes.mall.admin.application.system.plugin.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.plugin.plugin.service.PluginService;
import com.appkubes.mall.admin.application.system.plugin.entity.dto.SystemPluginDTO;
import com.appkubes.mall.admin.application.system.plugin.entity.vo.GetPluginVO;
import com.appkubes.mall.admin.application.system.plugin.service.SystemPluginService;
import com.appkubes.mall.core.entity.MallPlugin;
import com.appkubes.mall.core.entity.MallSystemPlugin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Rongjie
 * @Date: 2020/1/18 16:26
 * @Description:
 */
@Api("系统插件模块")
@RestController
@RequestMapping("/system/plugin")
public class SystemPluginController {
    @Resource
    SystemPluginService systemPluginService;
    @Resource
    PluginService mallPluginService;


    @ApiOperation("获取系统插件列表")
    @GetMapping("/get")
    public Response<List<SystemPluginDTO>> get(@ParamValid GetPluginVO getPluginVO) {
        MallSystemPlugin query = new MallSystemPlugin();
        query.setPluginType(getPluginVO.getPluginType());
        List<MallSystemPlugin> pluginList = systemPluginService.selectListByEntity(query);

        List<SystemPluginDTO> resultList = pluginList.stream().map(mallSystemPlugin -> {
            SystemPluginDTO pluginDTO = new SystemPluginDTO();
            BeanUtils.copyProperties(mallSystemPlugin, pluginDTO);

            //检查系统是否已经开通过了
            MallPlugin plugin = mallPluginService.getPlugin(getPluginVO.getAppKey(), mallSystemPlugin.getPluginCode());
            Integer isOpen = plugin == null ? 0 : 1;
            pluginDTO.setIsOpen(isOpen);

            return pluginDTO;
        }).collect(Collectors.toList());
        return Response.success(resultList);
    }


}
